﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class XAudioPostprocessor : AssetPostprocessor
{
    void OnPreprocessAudio()
    {
        AudioImporter ai = (AudioImporter)assetImporter;

        AudioImporterSampleSettings settings = new AudioImporterSampleSettings();
        settings.compressionFormat = AudioCompressionFormat.Vorbis;
        settings.loadType = AudioClipLoadType.CompressedInMemory;
        settings.quality = 0.5f;

        ai.forceToMono = true;
        ai.defaultSampleSettings = settings;
    }

    static string GetOutFile(string path)
    {
        return path.Replace("Assets/Art", "Assets/Resources").Replace(".ogg", ".prefab");
    }

    static string GetOutPath(string path)
    {
        return Path.GetDirectoryName(GetOutFile(path));
    }

    void OnPostprocessAudio(AudioClip clip)
    {
        AudioImporter ai = (AudioImporter)(assetImporter);
        GenerateAudioPrefab(clip, ai.assetPath);
    }

    [MenuItem("Assets/Generate Audio Prefab")]
    public static void GenerateSoundPrefab()
    {
        for (int i = 0; i < Selection.assetGUIDs.Length; i++)
        {
            string guid = Selection.assetGUIDs[i];
            string path = AssetDatabase.GUIDToAssetPath(guid);
            Debug.LogError(path);

            CreateAudioPrefab(path);
        }
    }

    public static void CreateAudioPrefab(string assetPath)
    {
        if (assetPath.Contains("."))
        {
            GenerateAudioPrefab(assetPath);
        }
        else
        {
            string[] aryFiles = Directory.GetFiles(Path.GetDirectoryName(assetPath), "*.ogg", SearchOption.AllDirectories);
            for (int i=0; i<aryFiles.Length; i++)
            {
                string file = aryFiles[i];
                if (file.Contains("."))
                {
                    EditorUtility.DisplayProgressBar("audio", file, (float)i / (float)aryFiles.Length);
                    GenerateAudioPrefab(file.Replace('\\', '/'));
                }
            }

            EditorUtility.ClearProgressBar();
        }
    }

    public static void GenerateAudioPrefab(AudioClip clip, string assetPath)
    {
        GameObject sourceGo = new GameObject(clip.name);
        if (sourceGo)
        {
            XAudioComponent audio = sourceGo.AddComponent<XAudioComponent>();
            audio.clip = clip;

            string outFile = GetOutFile(assetPath);
            string outPath = GetOutPath(outFile);
            if (!Directory.Exists(outPath))
                Directory.CreateDirectory(outPath);

            GameObject prefab = PrefabUtility.SaveAsPrefabAsset(sourceGo, outFile);
            if (prefab)
                Debug.LogError(outFile);

            GameObject.DestroyImmediate(sourceGo);
        }
    }

    public static void GenerateAudioPrefab(string assetPath)
    {
        AudioClip clip = AssetDatabase.LoadAssetAtPath<AudioClip>(assetPath);
        if (clip)
        {
            GenerateAudioPrefab(clip, assetPath);
        }
    }
}
